package com.yx.cloud.utils;
import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.yx.cloud.R;
import com.yx.cloud.adapter.MultiMapListAdapter;
import com.yx.cloud.bean.MapManagerItemBean;

import java.util.ArrayList;
import java.util.List;

public class PopupWindowRight extends PopupWindow implements MultiMapListAdapter.InnerItemOnclickListener{
    private static final String TAG = "PopupWindowRight";

    RecyclerView recyclerView;
    private Context context;
    private PopupWindowRight.ClickCallback clickCallback;
    private  View view;
    private List<MapManagerItemBean> multiMaplist;
    private MultiMapListAdapter multiMapListAdapter;
    private FragmentManager fmm;

    public PopupWindowRight(Context context,int viewId, List<MapManagerItemBean> multiMaplist) {
        this.context = context;
        this.multiMaplist = multiMaplist;
        //设置view
        LayoutInflater inflater = LayoutInflater.from(context);
        view = inflater.inflate(viewId, null);
        setContentView(view);
        initView();
        //activity的contentView的高度
        int height = ((Activity) context).findViewById(android.R.id.content).getHeight();

        height = height - dp2px( 44) - getStatusBarHeight((Activity) context);
        //其他设置
        setWidth(dp2px(154));//必须设置宽度
        setHeight(height);//必须设置高度
        setFocusable(false);//是否获取焦点
        setOutsideTouchable(true);//是否可以通过点击屏幕外来关闭
    }

    private int getStatusBarHeight(Activity activity) {
        int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            return activity.getResources().getDimensionPixelSize(resourceId);
        }
        return 150;
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
    }

    /**
     * 获取屏幕宽高
     *
     * @param context
     * @return
     */
    private static int[] getScreenSize(Context context) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        return new int[]{displayMetrics.widthPixels, displayMetrics.heightPixels};
    }

    private int getStatusBarHeight() {
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }


    /**
     * Value of dp to value of px.
     *
     * @param dpValue The value of dp.
     * @return value of px
     */
    public static int dp2px(final float dpValue) {
        final float scale = Resources.getSystem().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    private void initView() {
        //设置线性布局管理器类型
        recyclerView = view.findViewById(R.id.rv_multi_map_recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(context, RecyclerView.VERTICAL, false));
        if(multiMaplist == null){
            multiMaplist = new ArrayList<>();
        }
        multiMapListAdapter = new MultiMapListAdapter(context,multiMaplist,fmm);
        multiMapListAdapter.setOnInnerItemOnClickListener(this);
        recyclerView.setAdapter(multiMapListAdapter);
    }

    public void updateData(List<MapManagerItemBean> list){
        multiMaplist = list;
        multiMapListAdapter.updateData(multiMaplist);
        Log.d(TAG, "extend is updateData");
    }

    public void selectItem(int position){
        multiMapListAdapter.selectItem(position);
    }

    public int getSelectedItem(){
        return multiMapListAdapter.getSelectedItem();
    }

    public ClickCallback getClickCallback() {
        return clickCallback;
    }

    public void setClickCallback(ClickCallback clickCallback) {
        this.clickCallback = clickCallback;
    }

    @Override
    public void itemClick(int position) {
        if(clickCallback != null){
            clickCallback.onSelected(position);
        }
    }


    public interface ClickCallback {
        void onSelected(int position);
    }
}



